//
//  LEOwnController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/11.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEOwnController.h"
#import "LEGoodStaffCell.h"
#import "LEServiceCell.h"
#import "LEResignButtonCell.h"
#import "LEStoreModel.h"
#import "MakerTools.h"

#import "LESettingController.h"
#import "LEEditController.h"
#import "LEEditPasswordController.h"
#import "LEOpenPickerTool.h"
#import "LELoginController.h"
#import "LENavigationController.h"

static NSString *const LEGoodStaffCellId = @"LEGoodStaffCell";
static NSString *const LEServiceCellId = @"LEServiceCell";
static NSString *const LEResignButtonCellId = @"LEResignButtonCell";

@interface LEOwnController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

// 图片数据源
@property(nonatomic,strong)NSArray* imageArray;
// 标题数据源
@property(nonatomic,strong)NSArray* titleArray;
// image
@property(nonatomic,strong)UIImage *selectedImage;
/**
 商店模型
 */
@property(nonatomic,strong)LEStoreModel *storeModel;

@end

@implementation LEOwnController

- (void)setupUI
{
    [super setupUI];
    
    self.title = @"个人中心";
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"own_setting"] style:UIBarButtonItemStylePlain target:self action:@selector(settingClick:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    self.tableView.backgroundColor = LEBackgroundColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"LEGoodStaffCell" bundle:nil] forCellReuseIdentifier:LEGoodStaffCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"LEServiceCell" bundle:nil] forCellReuseIdentifier:LEServiceCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"LEResignButtonCell" bundle:nil] forCellReuseIdentifier:LEResignButtonCellId];
    self.tableView.bounces = NO;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:NO];
    [self loadData];
}
- (void)loadData
{
    NSArray *imageArr = @[@"my_4",@"service_moneyDescend",@"service_reply",@"service_commit"];
    _imageArray = imageArr;
    NSArray *titleArr = @[@"登录账号",@"地址",@"店铺电话",@"主营商品"];
    _titleArray = titleArr;
    
    //从沙盒中读取用户信息
    LEStoreModel *storeModel = [MakerTools readFromSandBox:Store_Info];
    [self.dataSource removeAllObjects];
    if (storeModel) {
        [self.dataSource addObject:storeModel.seller_name];
        [self.dataSource addObject:storeModel.store_address];
        [self.dataSource addObject:storeModel.store_tel];
        [self.dataSource addObject:storeModel.store_zy];
    }
    self.storeModel = storeModel;
    
    [self.tableView reloadData];
    
}
#pragma mark- 网络请求

- (void)requestUploadImage
{
    //设置参数
    NSData *data = UIImageJPEGRepresentation(self.selectedImage, 0.5);

    [SVProgressHUD showWithStatus:@"上传图片"];
     __weak typeof (self) weakSelf = self;
    [API netImage:OwnUploadStoreImageUrl andData:data imageName:@"store_label" success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
                return;
            }
            NSString *img_path = datas[@"img_path"];
            weakSelf.storeModel.image_url = img_path;
            [weakSelf updateLocalStoreInfo];
            
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
        }

        
    } falure:^(NSError *er) {
        [SVProgressHUD dismiss];
    }];
}

#pragma mark- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 4;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 120;
    }
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        LEGoodStaffCell *cell = [tableView dequeueReusableCellWithIdentifier:LEGoodStaffCellId forIndexPath:indexPath];
        if (self.storeModel) {
            cell.storeModel = self.storeModel;
        }
        return cell;
    }
    if (indexPath.section == 1) {
        LEServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:LEServiceCellId forIndexPath:indexPath];
        NSString *imageStr = self.imageArray[indexPath.row];
        cell.icon.image = [UIImage imageNamed:imageStr];
        cell.titleLabel.text = self.titleArray[indexPath.row];
        cell.contentLabel.hidden = NO;
        cell.contentLabel.text = self.dataSource[indexPath.row];
        return cell;
    }
    if (indexPath.section == 2) {
        LEServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:LEServiceCellId forIndexPath:indexPath];
        cell.icon.image = [UIImage imageNamed:@"service_sendDescend"];
        cell.titleLabel.text = @"修改密码";
        cell.contentLabel.hidden = YES;
        return cell;
    }else{
        LEResignButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:LEResignButtonCellId forIndexPath:indexPath];
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        LELog(@"%s---%d---更改用户头像",__func__,__LINE__);
        dispatch_async(dispatch_get_main_queue(), ^{
            [LEOpenPickerTool openPickerView:self];
        });
    }
    if (indexPath.section == 1) {
        LEEditController *edit = [[LEEditController alloc] init];
        [self.navigationController pushViewController:edit animated:YES];
    }
    if (indexPath.section == 2) {
        LEEditPasswordController *editPassword = [[LEEditPasswordController alloc] init];
        [self.navigationController pushViewController:editPassword animated:YES];
    }
    if (indexPath.section == 3) {
        LELog(@"%s---%d---退出登录",__func__,__LINE__);
        LELoginController *login = [[LELoginController alloc] init];
        LENavigationController *nav = [[LENavigationController alloc] initWithRootViewController:login];
        [UIApplication sharedApplication].delegate.window.rootViewController = nav;
        //清除用户信息
        [MakerTools clearUserInfo];
    }
}

#pragma mark- 事件
- (void)settingClick:(UIButton *)sender
{
    LELog(@"%s---%d---点击设置",__func__,__LINE__);
    LESettingController *setting = [[LESettingController alloc] init];
    setting.modalPresentationStyle = UIModalPresentationCustom;
    [self.navigationController pushViewController:setting animated:YES];
}
#pragma mark- imagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        self.selectedImage = image;
        [self requestUploadImage];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark- privateMethod

/**
 更新商店信息
 */
- (void)updateLocalStoreInfo
{
    
    [MakerTools saveToSandBox:Store_Info withSavedObject:self.storeModel];
    [self.tableView reloadData];
}
@end












